<?php

class academic_Model_DbTable_DbAcademic extends Zend_Db_Table_Abstract
{
	protected $_name ="tbwu_academic_year";
	
	public function getAcademicShow(){
		$db = $this->getAdapter();
		$sql = "SELECT ac.academic_year_id,ac.academic_year,ac.status ,
				(SELECT u.fullname FROM tbwu_acl_user AS u WHERE u.user_id = ac.user_id LIMIT 1) AS user_name ,
				(SELECT b.branch_name FROM tbwu_branch AS b WHERE b.branch_id = ac.branch_id LIMIT 1) AS branch_name
				FROM tbwu_academic_year AS ac ORDER BY ac.academic_year_id DESC";
		return $db->fetchAll($sql);
	}
	public function getAllAcademin($id){
		$db = $this->getAdapter();
		$sql = "SELECT * FROM $this->_name WHERE academic_year_id = $id";
		return $db->fetchRow($sql);
	}
    public function addAcademic($data){
    	$session = new Zend_Session_Namespace('auth');
    	$GetUserId = $session->user_id;
    	$branch_id = $session->branch;
    	$user_type = $session->level;
    	if($user_type==1){
    		$branch=$data["branch_name"];
    	}else {
    		$branch = $branch_id;
    	}
    	try {
	    	$arr = array(
	    		'branch_id'		=>	$branch,
	    		'academic_year'	=>	$data["academic_year"],
	    		'status'			=>	$data["status"],
	    		'created_date'		=>	new Zend_Date(),
	    		'user_id'			=>	$GetUserId,
	    		
	    	);
	    	$this->insert($arr);
    	}catch (Exception $e){
    		echo $e->getMessage();
    		exit();
    	}
    }
	public function updatYear($data,$id){
    	$session = new Zend_Session_Namespace('auth');
    	$GetUserId = $session->user_id;
    	$branch_id = $session->branch;
    	$user_type = $session->level;
    	if($user_type==1){
    		$branch=$data["branch_name"];
    	}else {
    		$branch = $branch_id;
    	}
    	try {
	    	$arr = array(
	    		'branch_id'		=>	$branch,
	    		'academic_year'	=>	$data["academic_year"],
	    		'status'			=>	$data["status"],
	    		'created_date'		=>	new Zend_Date(),
	    		'user_id'			=>	$GetUserId,
	    		
	    	);
	    	$where = $this->getAdapter()->quoteInto(" academic_year_id =? ",$id);
	    	$this->update($arr, $where);
    	}catch (Exception $e){
    		echo $e->getMessage();
    		exit();
    	}
    }
    public function updateStatus($arr)
    {
    	$array_data = array(
    			"status"	=>	1
    	);
    	$where = $this->getAdapter()->quoteInto('academic_year_id=?', $arr["unstatus"]);
    	$this->update($array_data, $where);
    		
    }
    public function updateUnStatus($arr)
    {
    	$array_data = array(
    			"status"	=>	0
    	);
    	$where = $this->getAdapter()->quoteInto('academic_year_id=?', $arr["status"]);
    	$this->update($array_data, $where);
    		
    }
    public function deleteYear($id) {
    	$db = $this->getAdapter();
    	$sql = "Delete From $this->_name WHERE academic_year_id=".$id;
    	$row = $db->query($sql);
    	return $row;
    }
}

